#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QListView>
#include <QtNetwork/QNetworkAccessManager>
#include <QUrl>
#include <QFileInfo>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QString>
#include <QStringList>
#include <QTimer>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QList>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{


    ui->setupUi(this);

getOrderListFromServer();







}

int MainWindow::getOrderListFromServer()
{
    manager= new QNetworkAccessManager();
    connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
    manager->get(QNetworkRequest(QUrl("http://globalcloud.me/testing/print/xml.php")));
// test
    return 0;
}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::replyFinished(QNetworkReply *reply)
{

    //some other code here
    // Reading attributes of the reply
        // e.g. the HTTP status code

        QVariant statusCodeV =reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
        // Or the target URL if it was a redirect:
        QVariant redirectionTargetUrl =
        reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
        // see CS001432 on how to handle this

        // no error received?
        if (reply->error() == QNetworkReply::NoError)
        {
            // read data from QNetworkReply here

            // Example 1: Creating QImage from the reply
            //QImageReader imageReader(reply);
          //  QImage pic = imageReader.read();

            // Example 2: Reading bytes form the reply
            QByteArray saveData = reply->readAll();  // bytes





          //  QJsonDocument loadDoc(QJsonDocument::fromJson(saveData));




            QJsonDocument d = QJsonDocument::fromJson(saveData);


                qWarning() << d.isNull();

                 //read(d.object());
                QJsonObject jsonobj=d.object();
            QString string(saveData); // string
            qDebug() << "Return Message";
            qDebug() << string;

            qDebug() <<  jsonobj["id"].toString();
        }
        // Some http error received
        else
        {
            // handle errors here
        }

        // We receive ownership of the reply object
        // and therefore need to handle deletion.
        delete reply;


}
